import { getAllCustomerApi } from "@/api/common";
import {
  getAllAccountTreeApi
  //  getDepositByCustomerIdApi
} from "@/api/financialManagement/receivable/waterVerification";
import { reactive, ref } from "vue";

export default function InitHook() {
  const formDataRef = ref<any>(null);
  const formState = reactive<WaterVerification.AuditDepositForm>({
    orderNum: "",
    customerId: "",
    depositPrice: undefined,
    paymentMethod: "",
    receivedRemark: "",
    serialNum: "",
    accountId: "",
    proceedsTime: "",
    payerName: "",
    image: [],
    remark: "",
    deptId: "",
    id: "",
    companyName: ""
  });
  const loading = ref<boolean>(false);
  const btnLoading = ref<boolean>(false);
  const customerOpts = ref<Array<Base.GeneraDataType>>([]);
  const accountOpts = ref<Array<Base.GeneraDataType>>([]);
  const companyOpts = ref<Array<Base.GeneraDataType>>([]);
  const getAllCustomer = () => {
    return getAllCustomerApi({ isFilterChild: 0 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        customerOpts.value =
          data.map((t: any) => {
            return {
              label: `${t.companyName}(${t.shortName})`,
              value: t.id
            };
          }) || [];
      }
    });
  };
  // 获取账户
  const getAccountOpts = () => {
    return getAllAccountTreeApi().then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        accountOpts.value = data.map((t: any) => {
          return {
            name: t.deptName,
            id: t.deptId,
            ...t
          };
        });
      }
    });
  };
  const filter = (inputValue: string, path: Array<Base.GeneraDataType>) => {
    return path.some(option => option.name.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
  };

  return {
    formDataRef,
    formState,
    loading,
    customerOpts,
    getAllCustomer,
    accountOpts,
    getAccountOpts,
    filter,
    btnLoading,
    companyOpts
  };
}
